function numberFormat(str) {
    str = '' + str.toFixed(2);
    var x = str.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while(rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
function formatprice(str) { return str ? '$' + numberFormat(str) : ''; }
function formatpercent(str) { return str ? numberFormat(str) + '%' : ''; }

function NtoW() {
 return false;
}

Elements.implement({
 filterByTag: function(tag) {
    return this.filter(tag);
 },
 filterByClass: function(className) {
    return this.filter('.' + className);
 },
 filterById: function(id) {
    return this.filter('#' + id);
 },
 filterByAttribute: function(name, operator, value) {
    return this.filter('[' + name + (operator || '') + (value || '') + ']');
 }
});


var CVTabs = new Class({
    Implements: Options,
    options: {
        tabsid: null,
        reload: true
    },
    tabs: null, // all tabs
    panels: null, // all panels
    initialize: function(options) {
        this.setOptions(options);
        this.tabs = document.id(this.options.tabsid);
        if(!this.tabs) return;

            this.panels = this.tabs.getElements('div.panel');

            this.addTabs({name: Cookie.read('user') || 'Guest', mask: Cookie.read('mask') || 0, seltab: Cookie.read('seltab') || 0 });
    },
    setTab: function(ev) {
        ev.stop();
        var id = document.id(ev.target).getParent().get('href');

        Cookie.write('seltab', document.id(ev.target).getParent().retrieve('tabindex'));

        this.tabs.getElements('a.tab').removeClass('active');
        this.panels.removeClass('active');

        this.tabs.getElements('a.tab').filterByAttribute('href', '=', id).addClass('active');
        this.panels.filterById(id).addClass('active');

        var tabArray = new Tabs();
        var func = tabArray.getFunction(id);
        if(typeOf(func) == 'function') {
            Function.attempt(func);
        }

    },
    addTabs: function(user) {

        document.id('suser').empty().set('text', 'Hello ' + user.name + '!');

        var tabArray = new Tabs();
        if(user.mask && user.mask != '0') {
            var tidx = 0;
            [0,1,2,3,4,5,6,7,8,9,10,11].each(function(shift, idx) {
                if(user.mask >>> shift & 1) {
                    var tab = tabArray.getTab(idx + 1);

                    new Element('li').adopt(
                        new Element('a.tab[href=' + tab.id + '][html=<span>' + tab.name + '</span>]', {
                            'events': {
                                'click': this.setTab.bind(this),
                                'focus': function() { this.blur(); }
                            }
                        }).store('tabindex', tidx++).store('ttab', tab)
                    ).inject(this.tabs.getFirst('ul'))
                }
            }, this);
        }
        else {
            new Element('li').adopt(
                new Element('a.tab[href=FP][html=<span>LogIn</span>]', {
                    'events': {
                        'click': this.setTab.bind(this),
                        'focus': function() { this.blur(); }
                    }
                }).store('tabindex', 0)
            ).inject(this.tabs.getFirst('ul'))
        }

        this.panels.removeClass('active');

        var selTab = this.tabs.getElements('a.tab')[user.seltab];
        selTab.addClass('active');

        document.id(selTab.get('href')).addClass('active');

        var ttab = selTab.retrieve('ttab');
        if(ttab) {
            Function.attempt(ttab.func);
        }

    },
    removeTab: function(idx) {

        this.tabs.getFirst('ul').empty();
    }
});

var IncludeTabs = new Class({
    Implements: Options,
    options: {
        tagid:null
    },
    tbs: null,
    tabs: null,
    panels: null,
    initialize: function(options) {
        this.setOptions(options);
        this.tbs = document.id(this.options.tagid);
        if(!this.tbs) return;

        this.tabs = this.tbs.getElements('a.itab');
        this.panels = this.tbs.getElements('div.ipanel');

        this.tabs.addEvents({
            'click': this.setTab.bind(this),
            'focus': function() {this.blur();}
        });
    },
    setTab: function(ev) {
        ev.stop();
        var id = document.id(ev.target).getParent().get('href');

        this.tabs.removeClass('iactive');
        this.panels.removeClass('iactive');

        this.tabs.filterByAttribute('href', '=', id).addClass('iactive');
        this.panels.filterById(id).addClass('iactive');

        switch(id) {
            case 'inVD':
                new EpyVendor({'tagid': id}); break;
            case 'inSUB':
                new EpyGroups({'tagid': id}); break;
            case 'inTST':
                new EpyItemTest({'tagid': id}); break;
            case 'inUSR':
                new EpyUser({ 'tagid': id}); break;
            case 'inUPD':
                new EpyUpdate({'tagid': id}); break;
        }
    }
});

